import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import sun.misc.Service;
import sun.net.spi.nameservice.NameService;
import sun.net.spi.nameservice.NameServiceDescriptor;

//-Djava.security.properties=F:\eclipse\nml2\dns.prop
public class MyNameService implements NameService {
	private static int sinaserno = 0;
	private static List<String> sinahost = new ArrayList<String>();

	private static int x163serno = 0;
	private static List<String> x163host = new ArrayList<String>();

	static {
		sinahost.add("114.134.80.164");
		sinahost.add("123.125.105.226");
		sinahost.add("180.149.153.227");
		sinahost.add("202.108.33.40");
		sinahost.add("219.142.78.204");
		sinahost.add("58.63.234.86");

		// ///////163
		x163host.add("123.125.50.97");
		x163host.add("220.181.72.147");
		
		
		
		
	}

	public static void main(String[] args) throws Exception {

		// bb=InetAddress.getLocalHost().getAddress()
		java.security.Security.setProperty("networkaddress.cache.ttl", "0");
		System.setProperty("sun.net.spi.nameservice.provider.1",
				"dns,MyNameService");

		Iterator itr = Service.providers(NameServiceDescriptor.class);

		while (itr.hasNext()) {
			NameServiceDescriptor nsd = (NameServiceDescriptor) itr.next();
			System.out.println(nsd.getType() + nsd.getProviderName());
		}

		for (int i = 0; i < 8; i++) {
			Connection conn = Jsoup.connect("https://login.sina.com.cn/");
			conn.get();
		}

	}

	@Override
	public String getHostByAddr(byte[] arg0) throws UnknownHostException {
		throw new UnknownHostException();
	}

	@Override
	public InetAddress[] lookupAllHostAddr(String arg0)
			throws UnknownHostException {
		InetAddress[] array = new InetAddress[1];

		if (arg0.equalsIgnoreCase("login.sina.com.cn")) {
			String ip = sinahost.get(sinaserno++);
			if (sinaserno == sinahost.size()) {
				sinaserno = 0;
			}
			array[0] = InetAddress.getByName(ip);
			System.out.println("use " + ip);
			return array;
		}
		
		if (arg0.equalsIgnoreCase("reg.email.163.com")) {
			String ip = x163host.get(x163serno++);
			if (x163serno == x163host.size()) {
				x163serno = 0;
			}
			array[0] = InetAddress.getByName(ip);
			System.out.println("use " + ip);
			return array;
		}

		
		if (arg0.equalsIgnoreCase("fmail.21cn.com")) {
			 
			array[0] = InetAddress.getByName("121.14.129.157");
			System.out.println("use 121.14.129.157"    );
			return array;
		}
		
		
		
		
		System.out.println("use " + " null");
		return null;

	}
}
